package com.SoapClasses;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.Tag;
import com.Managers.LogManagerClass;


public class SoapPost_Tags {

    private static final String TAG = "SOAP_TAG";

    private String METHOD_NAME;

    private String SOAP_ACTION;

    SoapSerializationEnvelope envelope;

    PropertyInfo pi;

    SoapObject Request;

    //
    public Tag[] getTagByName(String startWith) {

        METHOD_NAME = "getTagBy";
        SOAP_ACTION = "http://ShareUp.com/getTagBy";
        // /////////////////////////////////////////////////////////////////////////
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("data", startWith);
        // //////////////////////////////////////////////////////////////////////////
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE trans = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        // //////////////////////////////////////////////////////////////////////////
        Object response = null;
        //
        try {
            trans.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Tag[] tg = new Tag[1];
            Tag ta = new Tag();
            ta.setName("-10");
            tg[0] = ta;
            return tg;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            Tag[] tg = new Tag[1];
            Tag ta = new Tag();
            ta.setName("-10");
            tg[0] = ta;
            return tg;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        //
        //
        return setTagNames(response);
    }

    public Tag[] getTagPopular() {

        String METHOD_NAME = "getTagPopular";
        String SOAP_ACTION = "http://ShareUp.com/getTagPopular";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Tag[] tg = new Tag[1];
            Tag ta = new Tag();
            ta.setName("-10");
            tg[0] = ta;
            return tg;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            Tag[] tg = new Tag[1];
            Tag ta = new Tag();
            ta.setName("-10");
            tg[0] = ta;
            return tg;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        return setTagPopular(response);

    }

    private Tag[] setTagNames(Object soap) {

        String RES = soap.toString();
        try {
            JSONArray jsonArray = new JSONArray(RES);
            Tag[] tgs = new Tag[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                Tag tg = new Tag();
                tg.setName(jo.getString("Name"));
                tgs[i] = tg;

            }
            return tgs;

        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return null;
        }

    }

    private Tag[] setTagPopular(Object soap) {

        String RES = soap.toString();
        try {
            JSONArray jsonArray = new JSONArray(RES);
            Tag[] tgs = new Tag[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                Tag tg = new Tag();
                tg.setName(jo.getString("Name"));
                tg.setCount(jo.getInt("Count"));
                tgs[i] = tg;

            }
            return tgs;

        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return null;
        }

    }
}
